package com.bw.bwskinfilecreator;

import com.bw.bwskinfilecreator.bean.ProjectConfigEntity;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;

public class ProjectConfigController {
    @FXML
    private RadioButton rb1;
    @FXML
    private RadioButton rb2;

    @FXML
    private TextField projectTargetTextField;
    private ToggleGroup toggleGroup;

    @FXML
    private TextField sourceFolderTextField;

    @FXML
    private TextField gitFolderTextField;
    private Stage dialogStage;
    private ProjectConfigEntity projectConfigEntity;
    private OnLiProjectActionListener onProjectActionListener;

    public void initialize() {
        loadProjectConfig();
        // 1. 创建ToggleGroup
        toggleGroup = new ToggleGroup();
        // 2. 关联RadioButton到ToggleGroup
        rb1.setToggleGroup(toggleGroup);
        rb2.setToggleGroup(toggleGroup);
        // 3. 设置默认选中项（可选）
        rb1.setSelected(true);
    }

    public void initData(ProjectConfigEntity projectConfigEntity) {
        this.projectConfigEntity = projectConfigEntity;

        if (projectConfigEntity != null) {
            projectTargetTextField.setText(projectConfigEntity.getProjectName());
            sourceFolderTextField.setText(projectConfigEntity.getSourceProjectPath());
            gitFolderTextField.setText(projectConfigEntity.getGitProjectPath());
            if (projectConfigEntity.getSkinFileName().equals("day.skin")) {
                rb1.setSelected(true);
            } else {
                rb2.setSelected(true);
            }
        }
    }

    /**
     * 加载项目配置
     */
    private void loadProjectConfig() {
        projectTargetTextField.setText(projectConfigEntity != null ? projectConfigEntity.getProjectName() : "");
        if (projectConfigEntity != null) {
            sourceFolderTextField.setText(projectConfigEntity.getSourceProjectPath());
            gitFolderTextField.setText(projectConfigEntity.getGitProjectPath());
            //名称
            String skinFileName = projectConfigEntity.getSkinFileName();
            if ("night.skin".equals(skinFileName)) {
                rb2.setSelected(true);
            } else {
                rb1.setSelected(true);
            }
        }
    }

    @FXML
    private void handleSourceBrowseButtonAction(ActionEvent event) {
        chooseDirectory(sourceFolderTextField, "选择源文件夹");
    }

    @FXML
    private void handleSaveButtonAction(ActionEvent event) {
        String projectName = projectTargetTextField.getText();
        String projectSourcePath = sourceFolderTextField.getText();
        String gitFolderPath = gitFolderTextField.getText();
        if (projectName == null || projectName.isEmpty()) {
            log("项目名称不能为空");
            return;
        }
        //其他提示
        if (projectConfigEntity == null) {
            projectConfigEntity = new ProjectConfigEntity();
        }
        projectConfigEntity.setProjectName(projectName);
        projectConfigEntity.setSourceProjectPath(projectSourcePath);
        projectConfigEntity.setGitProjectPath(gitFolderPath);
        projectConfigEntity.setSkinFileName(rb1.isSelected() ? "day.skin" : "night.skin");

        if (onProjectActionListener != null) {
            onProjectActionListener.onSaveClick(projectConfigEntity);
        }
    }

    @FXML
    private void handleCancelButtonAction(ActionEvent event) {
        //关闭弹窗
        if (onProjectActionListener != null) {
            onProjectActionListener.onCancelClick();
        }
    }

    @FXML
    private void handleGitBrowseButtonAction(ActionEvent event) {
        chooseDirectory(gitFolderTextField, "选择Git上传路径");
    }

    private void chooseDirectory(TextField textField, String title) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle(title);

        // 设置初始目录，如果无效则使用默认目录
        String currentPath = textField.getText();
        File initialDirectory;
        
        if (currentPath != null && !currentPath.trim().isEmpty()) {
            initialDirectory = new File(currentPath);
            // 检查路径是否存在且是目录
            if (!initialDirectory.exists() || !initialDirectory.isDirectory()) {
                initialDirectory = new File("C:\\Users");
            }
        } else {
            initialDirectory = new File("C:\\Users");
        }
        
        // 最后检查默认目录是否有效
        if (!initialDirectory.exists() || !initialDirectory.isDirectory()) {
            initialDirectory = new File(System.getProperty("user.home"));
        }
        
        directoryChooser.setInitialDirectory(initialDirectory);

        File selectedDirectory = directoryChooser.showDialog(dialogStage);
        if (selectedDirectory != null) {
            textField.setText(selectedDirectory.getAbsolutePath());
        }
    }

    private void log(String msg) {
        if (onProjectActionListener != null) {
            onProjectActionListener.onLog(msg);
        }
    }

    public void setOnProjectActionListener(OnLiProjectActionListener onProjectActionListener) {
        this.onProjectActionListener = onProjectActionListener;
    }

    public interface OnLiProjectActionListener {
        void onSaveClick(ProjectConfigEntity projectConfigEntity);

        void onCancelClick();

        void onLog(String msg);
    }
}
